package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 *
 * @author atguigu
 * @since 2023-08-30
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseTrademarkService baseTrademarkService;

    /**
     * 根据category3Id获取品牌列表
     *
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getTrademarkList(Long category3Id) {
        //1.根据分类ID查询中间表得到分类品牌集合
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        List<BaseCategoryTrademark> categoryTrademarkList = this.list(queryWrapper);

        //2.获取分类品牌集合中品牌ID集合
        if (!CollectionUtils.isEmpty(categoryTrademarkList)) {
            //采用Stream流获取集合中元素品牌ID   将集合泛型从BaseCategoryTrademark转为Long类型
            List<Long> tradeMarkIdList = categoryTrademarkList.stream()
                    .map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //3.根据品牌ID集合查询品牌列表
            List<BaseTrademark> trademarkList = baseTrademarkService.listByIds(tradeMarkIdList);
            return trademarkList;
        }
        return null;
    }

    /**
     * 根据category3Id获取可选品牌列表(未关联到分类品牌列表)
     *
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
        return baseMapper.getCurrentTrademarkList(category3Id);
    }

    /**
     * 将多个品牌关联到分类
     *
     * @param vo
     * @return
     */
    @Override
    public void saveCategoryTradeMark(CategoryTrademarkVo vo) {
        //1.获取品牌ID集合
        List<Long> trademarkIdList = vo.getTrademarkIdList();

        //2.将品牌ID集合转为分类品牌对象集合
        if (!CollectionUtils.isEmpty(trademarkIdList)) {
            List<BaseCategoryTrademark> list = trademarkIdList.stream().map(trademarkId -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(trademarkId);
                baseCategoryTrademark.setCategory3Id(vo.getCategory3Id());
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            //3.批量保存
            this.saveBatch(list);
        }
    }


    /**
     * 解除品牌关联分类
     *
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @Override
    public void deleteTIdFromCategory(Long category3Id, Long trademarkId) {
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id)
                .eq(BaseCategoryTrademark::getTrademarkId, trademarkId);
        this.remove(queryWrapper);
    }
}
